/**
 * Created by jonny on 12/23/15.
 */
app.controller('lineOrderContorller', ['$scope', 'ajax', '$state', 'Flash','$filter','$stateParams','sellService','SERVER_URL', function ($scope, ajax, $state, Flash,$filter,$stateParams,sellService,SERVER_URL) {

    $scope.pageInfo = {
        currentPage: 0,
        totalPageSize: 0,//总页数
        pageSize: 10
    };
    $scope.queryData = {
        orderNo:'',
        orderId: "",
        ctime_tstart: "",
        ctime_tend: "",
        customerId: "",
        payType:"",
        purchaseOrderId:$stateParams.id?$stateParams.id:'',
        purchaseStatus_start : 1,
        orderStatus: "",
        currentPage: 1,
        pageSize: 10,
        purchaseStatus:'',
        sellerCode:'',
        isShowOrderDetail:true
    };

    $scope.data = {
        isShowDetail: false,//是否显示详细
        orderId: "",
        customerId: "",
        isShowQueryInfo : $stateParams.id?false:true
    };

    $scope.arr=[];//单选按钮的orderid数组
    $scope.isChecked=false;
    //全选
    $scope.isTrueChecked=function(isChecked,orderId){
       if(isChecked){
           $scope.arr.push(orderId);
       }
    }
    $scope.checkAll=function(allChecked){
        if(allChecked){
            $scope.isChecked=true;
            $scope.arr.push($scope.arrOrderId);
        }
       else{
            $scope.isChecked=false;
        }
    }
    //导出按钮
    $scope.export=function(){
        var queryData = 'orderNo='+$scope.queryData.orderNo+'&customerId='+$scope.queryData.customerId+'&orderId='+$scope.queryData.orderId+'&ctime_tstart='+$scope.queryData.ctime_tstart+'&ctime_tend='+$scope.queryData.ctime_tend+'&payType='+$scope.queryData.payType
        location.href=SERVER_URL+'/back/order/allOrderExportExcel?'+queryData;

    }

    //重置按钮
    $scope.reset=function(){

        $scope.queryData={};
        $scope.isclick=true;

    }

    $scope.resultList = {};
    //查询
    $scope.doQuery = function (sellerCode) {

        var ctime_tstart=$filter('date')($scope.queryData.ctime_tstart,'yyyy-MM-dd');
        $scope.queryData.ctime_tstart=ctime_tstart;
        var ctime_tend=$filter('date')($scope.queryData.ctime_tend,'yyyy-MM-dd');
        $scope.queryData.ctime_tend=ctime_tend;
        console.log($scope.queryData);
        if(angular.isUndefined(sellerCode) || $scope.isclick==true){
            getListByPage();
        }
        else{
            $scope.queryData.sellerCode= sellerCode.sellerCode;
            getListByPage();
        }
        $scope.isclick=false;
    };

    getListByPage();
    $scope.goPrePage = function () {

        if ($scope.queryData.currentPage <= 1) {
            $scope.queryData.currentPage = 1;
        } else {
            $scope.queryData.currentPage -= 1;
        }
        getListByPage();
    };

    $scope.goNextPage = function () {

        if ($scope.queryData.currentPage >= $scope.pageInfo.totalPageSize) {
            $scope.queryData.currentPage = $scope.pageInfo.totalPageSize;

        } else {
            $scope.queryData.currentPage += 1;
        }

        getListByPage();

    };

    function getListByPage(completeCallback) {
        //console.log($scope.queryData)
        if($stateParams.id){
            isShowOrderDetail:false
        }
        ajax.get({
            url: "back/order/page",
            data: $scope.queryData
        }).then(function (result) {

            var arrOrderId=[];//全选按钮的orderid数组
            result.resultData.forEach(function(item){
                arrOrderId.push(item.orderId);
            })
            $scope.arrOrderId=arrOrderId;
            $scope.pageInfo.currentPage = result.currentPage;
            $scope.pageInfo.totalPageSize = result.totalPageSize;
            $scope.resultList = result.resultData;
            $scope.totalSize=result.totalSize;
            $scope.trueTotalPrice=result.trueTotalPrice;
            if (completeCallback) {
                completeCallback(result);
            }

        });

    }

    $scope.orderDetailList = [];

    //查询订单详情列表
    $scope.queryOrderDetail = function (orderId, customerId,orderStatus) {
        $scope.orderId=orderId;//保存orderId以便添加商品
        ajax.get({
            url: "/back/order/orderDetailList",
            data: {
                orderId: orderId
            }
        }).then(function (result) {
            $scope.data.isShowDetail = true;
            $scope.data.orderId = orderId;
            $scope.data.customerId = customerId;
            $scope.queryData.orderStatus=orderStatus;
            $scope.orderDetailList = result;

        });
    }

    //前端删除订单指定商品
    $scope.deleteOrder = function (id) {
        if(!confirm('是否操作')){
            return false
        }
        ajax.post({
            url: "/back/order/delete",
            data: {
                orderId: id
            }
        }).then(function () {
            var newArr = new Array();
            for (var i = 0, listLength = $scope.orderDetailList.length; i < listLength; i++) {
                if ($scope.orderDetailList[i].commodityId != id) {
                    newArr.push($scope.orderDetailList[i]);
                }
            }
            $scope.orderDetailList = newArr;
            Flash.create('success', '删除成功', 'custom-class');
            $state.go('admin.lineOrder',null,{
                reload:true
            });

        });


    };

    //查询销售人员
    function list(page){
        sellService.list({
            currentPage: page,
            pageSize: 999
        }).then(function(result){
            $scope.seller = result.resultData;
        })
    };
    list(1);

    //查询商品相关
    $scope.goodsPageInfo = {
        currentPage: 0,
        totalPageSize: 0,//总页数
        pageSize: 10
    };

    $scope.goodsQueryData = {
        currentPage: 0,
        pageSize: 10
    };

    $scope.goodsResultList = {};

    $scope.doQueryGoods = function () {

        $scope.goodsQueryData.currentPage = 1;

        getGoodsListByPage();
    };

    $scope.doQueryGoods();

    $scope.goGoodsPrePage = function () {
        if ($scope.goodsQueryData.currentPage <= 1) {
            $scope.goodsQueryData.currentPage = 1;
        } else {
            $scope.goodsQueryData.currentPage -= 1;
        }
        getGoodsListByPage();
    };

    $scope.goGoodsNextPage = function () {
        if ($scope.goodsQueryData.currentPage >= $scope.goodsPageInfo.totalPageSize) {
            $scope.goodsQueryData.currentPage = $scope.goodsPageInfo.totalPageSize;
        } else {
            $scope.goodsQueryData.currentPage += 1;
        }

        getGoodsListByPage();

    }

    function getGoodsListByPage(completeCallback) {

        ajax.get({
            url: "back/commodity/page",
            data: $scope.queryData
        }).then(function (result) {
            $scope.goodsPageInfo.currentPage = result.currentPage;
            $scope.goodsPageInfo.totalPageSize = result.totalPageSize;

            $scope.goodsResultList = result.resultData;
            if (completeCallback) {
                completeCallback(result);
            }
        });
    }

    //添加商品
    $scope.addGoods = function (index) {
        var good = $scope.goodsResultList[index];

        //判断商品是否已存在
        for (var i = 0; i < $scope.orderDetailList.length; i++) {
            if (good.commodityId == $scope.orderDetailList[i].commodityId) {
                Flash.create('success', "请勿重复添加商品！", 'custom-class');
                return;
            }
        }

        var obj = {
            commodityId: good.commodityId,
            commodityName: good.name,
            number: 0
        };

        $scope.orderDetailList.push(obj);
    };


    //根据id导出表格
    $scope.exportExcel = function (id) {
        location.href=SERVER_URL+'back/order/exportExcel?orderId='+id;
    };


    //修改实际数量
    $scope.updateNumber=function(orderDetailId,trueNumber){
        ajax.post({
            url: "back/order/updateOrderDetailTrueNumber",
            data: {
                orderDetailId: orderDetailId,
                trueNumber: trueNumber
            }
        }).then(function (result) {
            Flash.create('success','修改实际数量成功', 'custom-class');
        });
    }
    //保存订单详细修改信息
    $scope.saveOrderDetailInfo=function(){
        $state.go('admin.lineOrder',null,{
            reload:true
        });
    }

    /*
     * 移出商品
     */
    $scope.removeCart = function (id) {
        $scope.detail.commoditys.forEach(function (item, index, array) {
            if (item.commodityId === id) {
                array.splice(index, 1);
            }
        })
    }

    /*
     * 添加商品
     */
    $scope.addCart = function () {
        ajax.post({
            url: "/back/order/addOrderDetail",
            data: {
                orderId:$scope.orderId,
                commoditys:$scope.detail.commoditys
            }
        }).then(function (result) {
            Flash.create('success','添加成功', 'custom-class');
            $state.go('admin.lineOrder',null,{
                reload:true
            });
        });

    }
    //选中的打印
    $scope.orderPrint=function(orderId){
        if(orderId.orderArr==''){
            Flash.create('success','请选中你要打印的订单', 'custom-class');
            return false
        }
        $state.go('orderPrint',{id:orderId.orderArr});
    }



}]);